const user = require("../databases/user");
const md5 = require("md5");
const uuid = require("uuid");

module.exports = (req, res, next)=>{
  const un = req.body.username;
  const pw = req.body.password;

  if(!pw){
    res.send({
      code:2,
      msg:"缺少关键参数：password"
    })
    return;
  }

  user.find({username: un}).then(data=>{
    if(data.length > 0){
      res.send( {
        code: 0,
        msg:"用户名重复"
      } );
    }else{
      user.insertMany({
        uId: "user-" + uuid.v1(),
        username: un,
        password: md5(pw),
        tel: "",
        nickName: "",
        age: 0,
        sex: "",
        regTime: Date.now(),
        power: 0,
        avatar: ""
      }).then(()=>{
        res.send( {
          code: 1,
          msg:"注册成功"
        } );
      })
    }
  })
}